package cn.tedu.yy;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;



/**
 * 面向对象版本， 同步删除
 */
public class Yang extends JFrame {
    private JPanel panel = new JPanel(null);
    private ArrayList<Card> cards = new ArrayList<>();
    private ArrayList<Card> though = new ArrayList<>();
    String[] names = {"刷子","剪刀","叉子","奶瓶","干草","手套","树桩",
            "棉花","毛线","水桶","火","玉米","白菜","草","萝卜","铃铛"};

    public Yang(){
        setTitle("羊了个羊");
        add(panel);
        setSize(490, 800);
        JLabel background = new JLabel(new ImageIcon("images/背景.jpg"));
        background.setSize(480,800);
        background.setLocation(0,0);
        panel.add(background);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public void action(){
        createCards(6);
        Collections.shuffle(cards);
        int n = deal(0, 6,7, 30,100);
        n += deal(n, 5,6, 60,133);
        n += deal(n, 5,7, 30,166);
        showUpperCards();
        addClickAction();
    }

    public void addClickAction(){
        for (int i=0 ;i<cards.size(); i++){
            Card card = cards.get(i);
            card.addActionListener(e -> {
                Card selected = (Card) e.getSource();
                System.out.println("点击："+selected);
                if (though.size() == 7){
                    JOptionPane.showMessageDialog(panel, "槽子满了！");
                    return;
                }
                cards.remove(selected);
                //去除按钮上的动作事件
                ActionListener l = selected.getActionListeners()[0];
                selected.removeActionListener(l);
                showUpperCards();
                addToThough(selected);
            });
        }
    }

    public void addToThough(Card selected){
        //查找插入位置
        int found = -1;
        for (int i=0; i<though.size(); i++){
            Card card = though.get(i);
            if (card.sameAs(selected)){
                though.add(i, selected);
                found = i;
                break;
            }
        }
        if (found == -1){
            though.add(selected);
        }
        //3 消除连续的3张牌
        if (found != -1){
            //如果 found 不是 -1，也就是中间插入，时候开始消除
            //当前位置开始，到最后至少有3张牌
            if (though.size()-found >= 3){
                Card nextNextCard = though.get(found+2);
                if (selected.getName().equals(nextNextCard.getName())){
                    panel.remove(though.remove(found));
                    panel.remove(though.remove(found));
                    panel.remove(though.remove(found));
                }
            }
        }
        //2. 根据排序的结果， 重新设置 每个牌的 Location
        showThough();
    }

    private void showThough() {
        for (int i = 0; i < though.size(); i++) {
            Card card = though.get(i);
            int x = 25 + 62 * i;
            int y = 642;
            card.setLocation(x, y);
        }
        //重新绘制panel，解决删除的残影
        panel.repaint();
    }

    public void createCards(int times){
        for (int i=0; i<times; i++){
            for(String name: names){
                cards.add(new Card(name));
            }
        }
    }
    public void showUpperCards(){
        for(int i=0; i<cards.size(); i++){
            Card card1 = cards.get(i);
            card1.setEnabled(true);
            for (int j=i+1; j<cards.size(); j++){
                Card card2 = cards.get(j);
                if (card2.covered(card1)) {
                    card1.setEnabled(false);
                }
            }
        }
    }
    public int deal(int start, int rows, int cols, int x, int y){
        int count = 0;
        for(int i=0; i<rows * cols ; i++){
            if (i+start == cards.size()){
                return count;
            }
            int x1 = x + i % cols * 59;
            int y1 = y + i / cols * 66;
            Card card = cards.get(i + start);
            card.setLocation(x1, y1);
            panel.add(card, 0);
            count++;
        }
        return count;
    }

    public void print(){
        int i=1;
        for(Card card:cards){
            System.out.print(card+" ");
            if (i++ % 7 == 0){
                System.out.println();
            }
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Yang yang = new Yang();
        yang.action();
        yang.setVisible(true);
    }
}
